Obstacle = {x = 0, y = 0, w = 10, h = 10, initialX = 0, initialY = 0}

local obstacleSprite = lg.newImage("heavyCloud.png")
obstacleSprite:setWrap("mirroredrepeat","repeat")

local obstacleColor = {165/255, 185/255, 201/255}

function Obstacle:new(x,y,w,h)
	--local obstacle = {x = x, y = y, w = w, h = h, initialX = x, initialY = y} or Obstacle
	local obstacle = {}
	obstacle.w = love.math.random(10,150)
	obstacle.h = love.math.random(10,150)
	obstacle.x = love.math.random(0, lg.getWidth() - obstacle.w - 10)
	obstacle.y = -obstacle.h
    obstacle.q = lg.newQuad(obstacle.x, obstacle.y, obstacle.w, obstacle.h, obstacleSprite)
	setmetatable(obstacle, self)
	self.__index = self
    table.insert(obstacles, obstacle)
	return obstacle
end

function Obstacle.update(self, dt)
	if timers.invicibility.isStopped and
      points[1] > self.x and points[1] < self.x + self.w and
      points[2] > self.y and points[2] < self.y + self.h then
		activated = false
        deathSFX:play()
		reset()
	end
	self.y = self.y + 40 * dt * speedMultiplier
	if self.y >= lg.getHeight() then
		local i = table.find(obstacles,self)
		table.remove(obstacles, i)
	end
end

local function drawOutline(self,color,offset)
    local offset = offset or 0
    local x = self.x - offset
    local y = self.y - offset
    local w = self.w + offset * 2
    local h = self.h + offset * 2
    lg.setColor(color)
    lg.rectangle("line",x,y,w,h)
    lg.circle("fill",x,y,4.5)
    lg.circle("fill",x + w, y, 4.5)
    lg.circle("fill",x, y+h, 4.5)
    lg.circle("fill",x + w, y+h, 4.5)
end

function Obstacle.draw(self)
    drawOutline(self,{0,0,0}, 3)
    drawOutline(self,obstacleColor)
    lg.setColor(1,1,1)
    lg.draw(obstacleSprite, self.q, self.x, self.y)
end

function Obstacle.reset(self)
	self.x = self.initialX
	self.y = self.initialY
end

return Obstacle
